package com.atguigu.statservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.RandomUtil;
import com.atguigu.statservice.client.UcenterClient;
import com.atguigu.statservice.entity.StatisticsDaily;
import com.atguigu.statservice.mapper.StatisticsDailyMapper;
import com.atguigu.statservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    
    @Autowired StatisticsDailyService statisticsDailyService;
    @Override
    public void registerCount(String day) {

        //添加记录前删除相同日期
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);


        //远程调用得到某一天注册人数
        R<Map<String, Object>> response = ucenterClient.countRegister(day);
        Integer countRegister = (Integer)response.getData().get("countRegister");
        //把获取数据添加数据库，统计分析表里面
        StatisticsDaily statistic = new StatisticsDaily();
        statistic.setDateCalculated(day);
        statistic.setRegisterNum(countRegister);

        statistic.setLoginNum(RandomUtils.nextInt(100,200));
        statistic.setLoginNum(RandomUtils.nextInt(100,200));
        statistic.setVideoViewNum(RandomUtils.nextInt(100,200));
        statistic.setCourseNum(RandomUtils.nextInt(20, 70));

        int insert = baseMapper.insert(statistic);



    }

    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated", type);

        List<StatisticsDaily> statList = baseMapper.selectList(wrapper);

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        ArrayList<String> dateCalculatedList = new ArrayList<>();
        ArrayList<Integer> numDataList = new ArrayList<>();

        statList.forEach((item) -> {

             dateCalculatedList.add(item.getDateCalculated());
             switch (type){
                 case "login_num":
                     numDataList.add(item.getLoginNum());
                     break;
                 case "register_num":
                     numDataList.add(item.getRegisterNum());
                     break;
                 case "video_view_num":
                     numDataList.add(item.getVideoViewNum());
                     break;
                 case "course_num":
                     numDataList.add(item.getCourseNum());
                     break;
                 default:
                     break;
             }
        });
        HashMap<String, Object>  map = new HashMap<>();
        map.put("date_calculatedList", dateCalculatedList);
        map.put("numDataList", numDataList);


        return map;
    }
}
